package com.hexiang.data.split.shardingsphere;

import com.hexiang.data.split.enums.DataSplitEnum;
import org.apache.shardingsphere.api.sharding.hint.HintShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.hint.HintShardingValue;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class DataSplitHintShardingKeyAlgorithm implements HintShardingAlgorithm<String> {

    private static final boolean GRAY_SWITCH = false;

    @Override
    public Collection<String> doSharding(Collection<String> availableTargetNames, HintShardingValue<String> hintShardingValue) {
        Collection<String> result = new ArrayList<>();
        List<String> availableTargetNameList = new ArrayList<>(availableTargetNames);
        if (GRAY_SWITCH) {
            result.add(availableTargetNameList.get(0));
            return result;
        }
        for (String tableName : availableTargetNames) {
            for (String shardingValue : hintShardingValue.getValues()) {

                if (shardingValue.equals(DataSplitEnum.HOT.value())) {
                    result.add(tableName);
                    return result;
                }
                if (shardingValue.equals(DataSplitEnum.COLD.value())) {
                    result.add(availableTargetNameList.get(1));
                    return result;
                } else {
                    result.add(tableName);
                }
            }
        }
        return result;
    }
}